package com.yunji.generate.fxml;

import com.yunji.generate.bean.TableInfo;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 选择数据表窗体
 *
 * @author Kevin
 * @since 2019/09/23.
 */
public class ChooseFrameController extends BaseController {

    private static final int DOUBLE_CLICK = 2;

    @FXML
    private TextField filterText;

    @FXML
    private TableView<TableInfo> table;

    @FXML
    private TableColumn<TableInfo, String> tableColumn1;

    @FXML
    private TableColumn<TableInfo, String> tableColumn2;

    private ObservableList<TableInfo> tableData = FXCollections.observableArrayList();

    private List<TableInfo> source;

    @FXML
    private void initialize() {
        tableColumn1.setCellValueFactory(new PropertyValueFactory<>("name"));
        tableColumn2.setCellValueFactory(new PropertyValueFactory<>("remarks"));
        table.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
        table.setItems(tableData);

        // 行双击选择事件
        table.setRowFactory(tv -> {
            TableRow<TableInfo> row = new TableRow<>();
            row.setOnMouseClicked(event -> {
                if (event.getClickCount() == DOUBLE_CLICK && (!row.isEmpty())) {
                    stage(event).setUserData(Collections.singletonList(row.getItem()));
                    hide(event);
                }
            });
            return row;
        });
    }

    @FXML
    public void onFilterTextKeyPressed(KeyEvent event) {
        if (event.getCode() == KeyCode.ENTER) {
            onFilterButtonClicked();
        }
    }

    @FXML
    public void onResetButtonClicked() {
        filterText.setText("");
        onFilterButtonClicked();
    }

    @FXML
    public void onChooseAllButtonClicked() {
        // 全选/全不选
        if (table.getSelectionModel().getSelectedItems().size() == tableData.size()) {
            table.getSelectionModel().clearSelection();
        } else {
            table.getSelectionModel().selectAll();
        }
    }

    @FXML
    public void onOkButtonClicked(ActionEvent event) {
        stage(event).setUserData(new ArrayList<>(table.getSelectionModel().getSelectedItems()));
        hide(event);
    }

    @FXML
    public void onFilterButtonClicked() {
        tableData.clear();
        String text = filterText.getText().trim().toUpperCase();
        if (text.isEmpty()) {
            tableData.addAll(source);
        } else {
            tableData.addAll(source.stream().filter((e) -> {
                if (e.getName().contains(text)) {
                    return true;
                }
                return e.getRemarks() != null && e.getRemarks().contains(text);
            }).collect(Collectors.toList()));
        }
    }

    @FXML
    public void onKeyPressed(KeyEvent event) {
        if (event.getCode() == KeyCode.ESCAPE) {
            // cancel
            hide(event);
        }
    }

    /**
     * 初始化表格数据
     */
    void initTableData(List<TableInfo> source) {
        this.source = source;
        this.tableData.clear();
        this.tableData.addAll(source);
        this.table.refresh();
    }
}
